#pragma once
#include <iostream>
#include <memory>
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

static const std::string sep = "\r\n";
// 设计一下协议的报头和报文的完整格式
// "len"\r\n"{json}"\r\n --- 完整的报文， len 有效载荷的长度！
// \r\n: 区分len 和 json 串
// \r\n: 暂时没有其他用，打印方便，debug
// 添加报头

std::string Encoede(const std::string &jsonstr)
{
    int len = jsonstr.size();
    std::string lenstr = std::to_string(len);
    return lenstr + sep + jsonstr + sep; // len\r\n{json}\r\n
}
std::string Decode(std::string &packagestream)
{
    auto pos = packagestream.find(sep);
    if (pos == std::string::npos)
    {
        return std::string();
    }
    std::string lenstr = packagestream.substr(0, pos);
    int len = std::stoi(lenstr);
    int total = lenstr.size() + len + 2 * sep.size();
    if (packagestream.size() < total)
    {
        return std::string();
    }

    std::string jsonstr = packagestream.substr(pos + sep.size(), len);
    packagestream.erase(0, total);
    return jsonstr;
}
class Request
{
private:
    int _x;
    int _y;
    char _oper;

public:
    Request()
    {
    }
    Request(int x, int y, char oper)
        : _x(x),
          _y(y),
          _oper(oper)
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        Json::FastWriter Writer;
        std::string s = Writer.write(root);
        *out = s;
        return true;
    }

    bool Deserialize(const std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool res = reader.parse(in, root);
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return true;
    }

    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _y << std::endl;
        std::cout << _oper << std::endl;
    }
    int X() // 外部无法直接调用私有，使用这个方法外部可以进行调用
    {
        return _x;
    }
    int Y()
    {
        return _y;
    }
    char OPER()
    {
        return _oper;
    }
    void SetValue(int x, int y, char oper)
    {
        _x = x;
        _y = y;
        _oper = oper;
    }
    ~Request()
    {
    }
};

class Response
{
public:
    int _result;
    int _code;
    std::string _desc;

public:
    Response(int result, int code, std::string desc)
        : _result(result),
          _code(code),
          _desc(desc)
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        root["desc"] = _desc;
        Json::FastWriter writer; // 更快的生成json字符串，但是没有格式化（换行，缩进）
        // Json::StyledWriter writer;   //相比性能稍低，但是会对字符串进行格式化
        std::string s = writer.write(root); // 将其序列化后写入s
        *out = s;
        return true;
    }

    bool Deserialize(const std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool res = reader.parse(in, root);
        if (!res)
        {
            return false;
        }
        _result = root["result"].asInt();
        _code = root["code"].asInt();
        _desc = root["desc"].asString();
        return true;
    }
    void PrintResult()
    {
        std::cout << "Result: " << _result << ", code: " << _code << ", desc: " << _desc << std::endl;
    }
    ~Response()
    {
    }
};

class Factory
{
public:
    static std::shared_ptr<Request> BuiledRequestDefault()
    {
        return std::make_shared<Request>();  //智能指针，返回一个指向 Request 对象的智能指针（shared_ptr）
    }
    static std::shared_ptr<Response> BuiledRespPonseDefault()
    {
        return std::make_shared<Response>();
    }

};
